package me.send.produce.data.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.data.service.pojo.UserBean;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.TreeService;
import me.send.data.service.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private TreeService treeService;

	// 查询提现订单列表
		@RequestMapping(value = "list")
		public ModelAndView user_list(HttpServletRequest request) {
			ModelAndView modelAndView = new ModelAndView(
					"stage/user/user_list");
			modelAndView.addObject("LoadTree",
					treeService.main_tree(0, request, "user/list"));
			modelAndView.addObject("module", "雇主列表");
			return modelAndView;
		}
		
		
		@RequestMapping(value="page", method=RequestMethod.GET)  
	    @ResponseBody  
	    public DatatablesViewPage<UserBean> datatablesTest(HttpServletRequest request){  
	//获取分页控件的信息  
	        String start = request.getParameter("start");  
	        String length = request.getParameter("length"); 
	//获取前台额外传递过来的查询条件  
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
	        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String start_time = request.getParameter("start_time"); 
	        String end_time = request.getParameter("end_time");  
	        String phone = request.getParameter("phone");  
	        String status = request.getParameter("status");  
	        String sex = request.getParameter("sex");  
	        Integer paramStatus=null;
	        Integer paramSex=null;
	        
	        if(status==null||(status!=null&&"".equals(status.trim()))){
	        	status=null;
	        }else {
	        	paramStatus=Integer.parseInt(status);
	        }
	        if(sex==null||(sex!=null&&"".equals(sex.trim()))){
	        	sex=null;
	        }else {
	        	paramSex=Integer.parseInt(sex);
	        }
	        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
	        	phone=null;
	        }
	        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
	        	Date startDate=null;
	        	Date endDate=null;
	        	try {
	        		startDate=sdf.parse(start_time);
	        		start_time=format.format(startDate);
	        		endDate=sdf.parse(end_time);
	        		end_time=format.format(endDate);
	        	} catch (ParseException e) {
	        		// TODO Auto-generated catch block
	        		e.printStackTrace();
	        	}
	        }else{
	        	start_time=null;
	        	end_time=null;
	        }
	        
	        Pager page=new Pager();
	        Integer row=Integer.parseInt(length);
	        page.setRows(row);
	        Integer startInt=Integer.parseInt(start);
	        Integer p=(startInt/row)+1;
	        page.setPage(p);
	        
	                //随便组织的查询结果  
	        List<UserBean> list=userService.list(phone,paramStatus,start_time,end_time,paramSex,page); 
	        Integer count=userService.count(phone,paramStatus,start_time,end_time,paramSex);
	        DatatablesViewPage<UserBean> view = new DatatablesViewPage<UserBean>();  
	        view.setiTotalDisplayRecords(count);  
	        view.setiTotalRecords(count);  
	          
	        view.setAaData(list);  
	        return view;  
	    }  
	
	

}
